package com.binhthuan.catechist.client.form;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.data.DateDS;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import com.smartgwt.client.widgets.form.validator.DateRangeValidator;

public class DateForm extends AbstractForm {

    private DateRangeValidator validator;

    public DateForm(DateDS dataSource) {
        super(dataSource);
        setMargin(10);
        setNumCols(4);
        setCellPadding(5);

        validator = new DateRangeValidator();

        HiddenItem idField = new HiddenItem(Parameter.ID);
        
        final SelectItem yearField = new SelectItem(Parameter.YEAR);
        yearField.setColSpan(4);
        yearField.addChangeHandler(new ChangeHandler() {
            
            @Override
            public void onChange(ChangeEvent event) {
                SelectItem owner = (SelectItem) event.getSource();
                Date startDate = owner.getSelectedRecord().getAttributeAsDate(Parameter.START_DATE);
                Date endDate = owner.getSelectedRecord().getAttributeAsDate(Parameter.END_DATE);
                yearField.setHint(ECApplication.getMessages().limitDateRange(ECApplication.formatDate(startDate), ECApplication.formatDate(endDate)));
                validator.setMin(startDate);
                validator.setMax(endDate);
            }
        });

        DateItem dateField = new DateItem(Parameter.DATE_OF_YEAR);
        dateField.setTitle(ECApplication.getConstants().schoolDate());
        dateField.setValidators(validator);

        TextItem liturgicalField = new TextItem(Parameter.LITURGICAL_DATE);
        liturgicalField.setTitle(ECApplication.getConstants().liturgicalDate());
        liturgicalField.setWidth(250);
        liturgicalField.setWrapTitle(false);

        ButtonItem saveButton = createSaveButton();
        saveButton.setColSpan(4);

        setFields(idField, yearField, dateField, liturgicalField, saveButton);
        addDrawHandler(new DrawHandler() {
            
            @Override
            public void onDraw(DrawEvent event) {
                SelectItem owner = (SelectItem) getItem(Parameter.YEAR);
                Date startDate = owner.getSelectedRecord().getAttributeAsDate(Parameter.START_DATE);
                Date endDate = owner.getSelectedRecord().getAttributeAsDate(Parameter.END_DATE);
                yearField.setHint(ECApplication.getMessages().limitDateRange(ECApplication.formatDate(startDate), ECApplication.formatDate(endDate)));
                validator.setMin(startDate);
                validator.setMax(endDate);
            }
        });
    }

    @Override
    protected void onUpdateFailure(DSResponse response, Object rawData) {
        Map<String, String> errors = new HashMap<String, String>();
        if (response.getStatus() == DSResponse.STATUS_VALIDATION_ERROR) {
            errors.put(Parameter.DATE_OF_YEAR, "The date is out of range of school year");
        } else if (response.getStatus() == DSResponse.STATUS_FAILURE) {
            errors.put(Parameter.DATE_OF_YEAR, "The date is duplicated");
        }
        setErrors(errors, true);
    }
}
